package com.review.backpack_01;

public class BP02 {
    //2. 完全背包问题
    //每件物品可以选无数次
    private int maxValue(int[] weight, int[] value, int bagWeight) {//改进做法
        int[] dp = new int[bagWeight + 1];
        for (int i = 0; i < weight.length; i++) {
            for (int j = weight[i]; j <= bagWeight; j++) {
                dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
            }
        }
        return dp[dp.length - 1];
    }

    private int maxValue2(int[] weight, int[] value, int bagWeight) {//常规做法
        int[] dp = new int[bagWeight + 1];
        for (int i = 0; i < weight.length; i++) {
            for (int j = bagWeight; j >= weight[i]; j--) {
                for (int k = 0; k * weight[i] <= j; k++) {
                    dp[j] = Math.max(dp[j], dp[j - k * weight[i]] + k * value[i]);
                }
            }
        }
        return dp[dp.length - 1];
    }

    public static void main(String[] args) {
        System.out.println("常规做法：" + new BP02().maxValue2(new int[]{1, 3, 4}, new int[]{15, 20, 30}, 4));
        System.out.println("改进做法：" + new BP02().maxValue(new int[]{1, 3, 4}, new int[]{15, 20, 30}, 4));
    }
}
